package cn.t.web.ent;

import cn.t.core.mode.PageInfoDTO;
import cn.t.core.mode.PageQueryDTO;
import cn.t.dto.user.RoleAssignParticipantRequest;
import cn.t.dto.user.RoleDeviceDTO;
import cn.t.dto.user.RoleInfoDTO;
import cn.t.dto.user.RoleSkuDTO;
import cn.t.facade.user.RoleDeviceFacade;
import cn.t.facade.user.RoleInfoFacade;
import cn.t.facade.user.RoleSkuFacade;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 企业站-权限组管理
 */
@Slf4j
@RestController
@RequestMapping("/ent/role")
@AllArgsConstructor
public class EntRoleController {

    private final RoleInfoFacade roleInfoFacade;
    private final RoleSkuFacade roleSkuFacade;
    private final RoleDeviceFacade roleDeviceFacade;

    /**
     * 权限组分页查询
     */
    @PostMapping("/page")
    public PageInfoDTO<RoleInfoDTO> entRolePage(@RequestBody PageQueryDTO<RoleInfoDTO> queryDTO) {
        return this.roleInfoFacade.entRolePage(queryDTO);
    }

    /**
     * 新建权限组
     */
    @PostMapping("/save")
    public Boolean add(@RequestBody RoleInfoDTO dto) {
        return roleInfoFacade.add(dto);
    }

    /**
     * 更新权限组
     */
    @PostMapping("/update")
    public Boolean update(@RequestBody RoleInfoDTO dto) {
        return this.roleInfoFacade.update(dto);
    }

    /**
     * 绑定领料员
     */
    @PostMapping("/assignParticipants")
    public Boolean assignParticipants(@Valid @RequestBody RoleAssignParticipantRequest request) {
        return roleInfoFacade.assignParticipants(request.getRoleId(), request.getUserIds());
    }



    /**
     * 绑定sku
     */
    @PostMapping("/bindSku")
    public Boolean bindSku(@RequestBody RoleSkuDTO dto) {
        if (dto.getId() == null) {
            return roleSkuFacade.add(dto);
        } else {
            return roleSkuFacade.update(dto);
        }
    }

    /**
     * 解绑sku
     */
    @PostMapping("/unbindSku/{id}")
    public Boolean unbindSku(@PathVariable Long id) {
        return roleSkuFacade.remove(id);
    }

    /**
     * 绑定设备
     */
    @PostMapping("bindDevice")
    public Boolean bindDevice(@RequestBody RoleDeviceDTO dto) {
        return roleDeviceFacade.add(dto);
    }

    /**
     * 解绑设备
     */
    @PostMapping("unbindDevice/{id}")
    public Boolean unbindDevice(@PathVariable Long id) {
        return roleDeviceFacade.remove(id);
    }

}

